<script setup lang="ts">
import { computed, onMounted } from 'vue'
import type { GenderOption } from '@/types/user-patient'

import { idCardRules, nameRules } from '@/utils/rules'

import { userPatient } from './composable/user-patient'
const { userPatientList, canAddUser, getUserPatientList } = userPatient()

import { operatePatient } from './composable/user-patient'

const {
  isEdit,
  showOperateDialog,
  operateForm,
  operateFormRef,
  defaultFlag,
  delPatient,
  onOperatePatient,
  onSubmitOperate,
  onCancelOperate
} = operatePatient(getUserPatientList)

const options: GenderOption[] = [
  { label: '男', value: 1 },
  { label: '女', value: 0 }
]

import { useRoute } from 'vue-router'

import { select } from './composable/user-patient'

const route = useRoute()

const title = computed(() => {
  return route.query.isChange ? '选择患者' : '家庭档案'
})

const { showNext, patientId, onSetSelectPatient, onToNext, storePatientId } =
  select(route)

const defineSelect = computed(() => {
  return (
    userPatientList.value &&
    (userPatientList.value.find((el) => el.defaultFlag)?.id ||
      userPatientList.value[0].id)
  )
})

onMounted(async () => {
  await getUserPatientList()
  if (route.query.isChange && userPatientList.value) {
    const defineId = storePatientId || defineSelect.value
    onSetSelectPatient(defineId!)
  }
})
</script>

<template>
  <div class="patient-page">
    <cp-nav-bar :title="title"></cp-nav-bar>
    <div class="patient-change" v-if="showNext">
      <h3>请选择患者信息</h3>
      <p>以便医生给出更准确的治疗，信息仅医生可见</p>
    </div>

    <div class="patient-list">
      <div
        class="patient-item"
        v-for="item in userPatientList"
        :key="item.id"
        :class="{ selected: patientId === item.id }"
        @click="() => onSetSelectPatient(item.id)"
      >
        <div class="info">
          <span class="name">{{ item.name }}</span>
          <span class="id">{{
            item.idCard.replace(idCardRules[1].pattern!, '$1********$2')
          }}</span>
          <span> {{ item.genderValue }} </span>
          <span>{{ item.age }}岁</span>
        </div>
        <div class="icon" @click="() => onOperatePatient(item.id)">
          <cp-icon name="user-edit" />
        </div>
        <div class="tag" v-if="item.defaultFlag">默认</div>
      </div>

      <div
        class="patient-add"
        v-if="canAddUser"
        @click="() => onOperatePatient()"
      >
        <cp-icon name="user-add" />
        <p>添加患者</p>
      </div>
      <div class="patient-tip">最多可添加 6 人</div>
    </div>

    <van-popup
      v-model:show="showOperateDialog"
      position="right"
      :close-on-popstate="true"
      style="width: 100vw; height: 100vh"
    >
      <div class="operate-page">
        <cp-nav-bar
          :title="`${isEdit ? '编辑' : '添加'}患者`"
          right-text="保存"
          :clickLeft="onCancelOperate"
          @click-right="onSubmitOperate"
        />

        <div class="operate-form">
          <van-form
            autocomplete="off"
            ref="operateFormRef"
            validate-trigger="onBlur"
          >
            <van-field
              label="真实姓名"
              placeholder="请输入真实姓名"
              v-model="operateForm.name"
              :rules="nameRules"
            />
            <van-field
              label="身份证号"
              placeholder="请输入身份证号"
              v-model="operateForm.idCard"
              :rules="idCardRules"
            />
            <van-field label="性别" class="pb4">
              <!-- 单选按钮组件 -->
              <template #input>
                <cp-radio-btn
                  :options="options"
                  v-model:value="operateForm.gender"
                />
              </template>
            </van-field>
            <van-field label="默认就诊人">
              <template #input>
                <van-checkbox :icon-size="18" round v-model="defaultFlag" />
              </template>
            </van-field>
          </van-form>
        </div>

        <div v-if="isEdit" class="del-btn-box" @click="delPatient">删除</div>
      </div>
    </van-popup>

    <van-action-bar v-if="showNext">
      <van-action-bar-button type="primary" text="下一步" @click="onToNext" />
    </van-action-bar>
  </div>
</template>

<style scoped lang="scss">
.patient-page {
  padding: 46px 0 80px;
}
.patient-list {
  padding: 15px;
}
.patient-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: var(--cp-bg);
  border-radius: 8px;
  margin-bottom: 15px;
  position: relative;
  border: 1px solid var(--cp-bg);
  transition: all 0.3s;
  overflow: hidden;
  &.selected {
    border-color: var(--cp-primary);
    background-color: var(--cp-plain);
    .icon {
      color: var(--cp-primary);
    }
  }
  .info {
    display: flex;
    flex-wrap: wrap;
    flex: 1;
    span {
      color: var(--cp-tip);
      margin-right: 20px;
      line-height: 30px;
      &.name {
        font-size: 16px;
        color: var(--cp-text1);
        width: 80px;
        margin-right: 0;
      }
      &.id {
        color: var(--cp-text2);
        width: 180px;
      }
    }
  }
  .icon {
    color: var(--cp-tag);
    width: 20px;
    text-align: center;
  }
  .tag {
    position: absolute;
    right: 60px;
    top: 21px;
    width: 30px;
    height: 16px;
    font-size: 10px;
    color: #fff;
    background-color: var(--cp-primary);
    border-radius: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.patient-add {
  background-color: var(--cp-bg);
  color: var(--cp-primary);
  text-align: center;
  padding: 15px 0;
  border-radius: 8px;
  .cp-icon {
    font-size: 24px;
  }
}
.patient-tip {
  color: var(--cp-tag);
  padding: 12px 0;
}
.pb4 {
  padding-bottom: 4px;
}

.patient-page {
  :deep(.van-popup) {
    .operate-page {
      width: 100%;
      height: 100%;
      padding-top: 46px;
      box-sizing: border-box;
      .del-btn-box {
        position: fixed;
        left: 0;
        bottom: 0;
        box-sizing: border-box;
        margin: 10px;
        width: calc(100% - 20px);
        height: 40px;
        text-align: center;
        line-height: 40px;
        border-radius: 25px;
        background-color: #f6f7f9;
        color: #eb5757;
      }
    }
  }
  :deep(.van-action-bar) {
    padding: 0 10px;
    margin-bottom: 20px;
    height: 44px;
    .van-button__content {
      .van-button__text {
        font-size: var(--van-button-normal-font-size);
      }
    }
  }
  .patient-change {
    padding: 15px;
    > h3 {
      font-weight: normal;
      margin-bottom: 5px;
    }
    > p {
      color: var(--cp-text3);
    }
  }
}
</style>
